package com.drinkdelivery.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.drinkdelivery.common.constants.ProductConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.drinkdelivery.mapper.ProductSpecValueMapper;
import com.drinkdelivery.common.domain.ProductSpecValue;
import com.drinkdelivery.service.IProductSpecValueService;

/**
 *  商品规格值Service业务层处理
 *
 * @author lzh
 * @date 2024-11-08
 */
@Service
public class ProductSpecValueServiceImpl extends ServiceImpl<ProductSpecValueMapper, ProductSpecValue> implements IProductSpecValueService
{
    @Autowired
    private ProductSpecValueMapper productSpecValueMapper;

    /**
     * 查询 商品规格值
     *
     * @param id  商品规格值主键
     * @return  商品规格值
     */
    @Override
    public ProductSpecValue selectProductSpecValueById(Long id)
    {
        //根据id获取未逻辑删除的数据
        LambdaQueryWrapper<ProductSpecValue> lqw = new LambdaQueryWrapper<>();
        lqw
                .eq(ProductSpecValue::getId,id)
                .eq(ProductSpecValue::getIsDeleted, 0);
        return this.getOne(lqw);
    }

    /**
     * 查询 商品规格值列表
     *
     * @param productSpecValue  商品规格值
     * @return  商品规格值
     */
    @Override
    public List<ProductSpecValue> selectProductSpecValueList(ProductSpecValue productSpecValue)
    {
        return productSpecValueMapper.selectProductSpecValueList(productSpecValue);
    }

    /**
     * 新增 商品规格值
     *
     * @param productSpecValue  商品规格值
     * @return 结果
     */
    @Override
    public Boolean insertProductSpecValue(ProductSpecValue productSpecValue)
    {
//        return productSpecValueMapper.insertProductSpecValue(productSpecValue);
        return this.save(productSpecValue);
    }

    /**
     * 修改 商品规格值
     *
     * @param productSpecValue  商品规格值
     * @return 结果
     */
    @Override
    public Boolean updateProductSpecValue(ProductSpecValue productSpecValue)
    {
//        return productSpecValueMapper.updateProductSpecValue(productSpecValue);
        return this.updateById(productSpecValue);
    }

    /**
     * 批量删除 商品规格值
     *
     * @param ids 需要删除的 商品规格值主键
     * @return 结果
     */
    @Override
    public boolean deleteProductSpecValueByIds(Long[] ids)
    {
        LambdaUpdateWrapper<ProductSpecValue> luw = new LambdaUpdateWrapper<>();
        luw
                //未逻辑删除
                .eq(ProductSpecValue::getIsDeleted,0)
                //遍历查询ids
                .in(ProductSpecValue::getId, Arrays.asList(ids))
                //进行逻辑删除
                .set(ProductSpecValue::getIsDeleted, 1);
        return this.update(luw);
    }

    /**
     * 删除 商品规格值信息
     *
     * @param id  商品规格值主键
     * @return 结果
     */
    @Override
    public Boolean deleteProductSpecValueById(Long id)
    {
        LambdaUpdateWrapper<ProductSpecValue> luw = new LambdaUpdateWrapper<>();
        luw.eq(ProductSpecValue::getId,id);
        luw.set(ProductSpecValue::getIsDeleted,1);
        return this.update(luw);
    }

    /**
     * 查询 商品规格值列表
     *
     * @param specId  商品规格属性id
     * @return  商品规格值集合
     */
    @Override
    public List<ProductSpecValue> selectProductSpecValueListBySpecId(Long specId) {
        LambdaQueryWrapper<ProductSpecValue> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ProductSpecValue::getSpecId,specId);
        lqw.eq(ProductSpecValue::getIsDeleted, ProductConstant.NO_DELETE);

        return this.list(lqw);
    }
}
